package com.up.stores.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.up.stores.entity.*;
import com.up.stores.mapper.*;
import com.up.stores.service.IOrderService;
import com.up.stores.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public Order1 createNorOrder(Integer nor_id, Integer pro_id, Integer num, String username) {
        /**
         * 先写order表再写order_item表
         */
        Product product=new Product();
        QueryWrapper<Product> queryWrapperProduct=new QueryWrapper<>();
        queryWrapperProduct.eq("pro_id",pro_id);
        product=productMapper.selectOne(queryWrapperProduct);
        if(product==null){
            throw new ProductNotFoundException("not found product");
            //抛出异常
        }
        Order1 order1 =new Order1();
        OrderItem orderItem=new OrderItem();

        //组装order和insert写入表格
        order1.setNorId(nor_id);
        Long price= product.getPrice() * num;
        order1.setTotalPrice(price);
        Date date=new Date();
        order1.setCreatedTime(date);
        order1.setStatus(0);
        order1.setStatus2(0);
        order1.setCreatedUser(username);
        orderMapper.insert(order1);

        //组装order_item,写入对应的表格
        Integer ord_id= order1.getOrdId();
        orderItem.setOrdId(ord_id);
        orderItem.setPid(pro_id);
        orderItem.setImage(product.getImageLong());
        orderItem.setPrice(product.getPrice());
        orderItem.setNum(num);
        orderItem.setTitle(product.getTitle());
        orderItem.setCreatedUser(username);
        orderItem.setCreatedTime(date);
        QueryWrapper<OrderItem> queryWrapperOrderItem=new QueryWrapper<>();
        orderItemMapper.insert(orderItem);

        return order1;
    }

    @Override
    public Order1 createAdmOrder(Integer adm_id, Integer pro_id, Integer num, String username ) {
        /**
         * 先写order表再写order_item表
         */
        Product product=new Product();
        QueryWrapper<Product> queryWrapperProduct=new QueryWrapper<>();
        queryWrapperProduct.eq("pro_id",pro_id);
        product=productMapper.selectOne(queryWrapperProduct);
        if(product==null){
            //抛出异常
            throw new ProductNotFoundException("商品不存在");

        }
        Order1 order1 =new Order1();
        OrderItem orderItem=new OrderItem();

        //组装order和insert写入表格
        order1.setNorId(adm_id);
        Long price= product.getPrice() * num;
        order1.setTotalPrice(price);
        Date date=new Date();
        order1.setCreatedTime(date);
        order1.setStatus(1);
        order1.setStatus2(0);
        order1.setCreatedUser(username);
        orderMapper.insert(order1);

        //组装order_item,写入对应的表格
        Integer ord_id= order1.getOrdId();
        orderItem.setOrdId(ord_id);
        orderItem.setPid(pro_id);
        orderItem.setImage(product.getImageLong());
        orderItem.setPrice(product.getPrice());
        orderItem.setNum(num);
        orderItem.setTitle(product.getTitle());
        orderItem.setCreatedUser(username);
        orderItem.setCreatedTime(date);
        QueryWrapper<OrderItem> queryWrapperOrderItem=new QueryWrapper<>();
        orderItemMapper.insert(orderItem);

        return order1;
    }

    @Override
    public void approvalOrder(Integer ord_id, Address address, Integer statues, Integer adm_id) {
        QueryWrapper<Order1> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ord_id",ord_id);
        Order1 order1 =orderMapper.selectOne(queryWrapper);
        QueryWrapper<Admin>queryWrapper1=new QueryWrapper<>();
        queryWrapper.eq("adm_id",adm_id);
        Admin admin=adminMapper.selectOne(queryWrapper1);
        if(order1 ==null){
            throw new OrderNotFoundException("not found order1");
            //抛出未查询到异常

        }
        if(admin==null){
            throw new AdminNotFoundException("not found admin");
        }
        order1.setAdmId(adm_id);
        Date date=new Date();
        order1.setModifiedTime(date);
        order1.setRecvProvince(address.getProvinceName());
        order1.setRecvCity(address.getCityName());
        order1.setRecvArea(address.getAreaName());
        order1.setRecvAddress(address.getAddress());
        order1.setRecvName(admin.getAdminName());
        orderMapper.update(order1,queryWrapper);
    }

    @Override
    public void changeOrderStatus(Integer ord_id,Integer status) {
        QueryWrapper<Order1> queryWrapper=new QueryWrapper();
        queryWrapper.eq("ord_id",ord_id);
        Order1 order1 =orderMapper.selectOne(queryWrapper);
        if(order1 ==null){
            throw new OrderNotFoundException("not found order1");
            //抛出未找到异常
        }
        //忘记改变的是哪一个状态了
        order1.setStatus2(status);
        Date date=new Date();
        order1.setModifiedTime(date);
        orderMapper.update(order1,queryWrapper);
        return;
    }

    @Override
    public List<Order1> findOrderByStatus1AndNorId(Integer nor_id, Integer status1) {
        QueryWrapper<Order1> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("nor_id",nor_id)
                .eq("status",status1);
        List<Order1> ordList=new ArrayList<>();
        ordList=orderMapper.selectList(queryWrapper);
        if(ordList.size()==0){
            throw new OrderListNotFoundException("order list not found");
            //没有查到，抛出异常

        }
        return ordList;
    }

    @Override
    public List<Order1> findOrderByStatus1AndAdmId(Integer adm_id, Integer status1) {
        QueryWrapper<Order1> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("adm_id",adm_id)
                .eq("status",status1);
        List<Order1> ordList = orderMapper.selectList(queryWrapper);
        if(ordList.size()==0){
            throw new OrderListNotFoundException("your order list not found");
            //没有查到，抛出异常

        }
        return ordList;
    }

    @Override
    public List<Order1> findOrderByNorId(Integer nor_id) {
        QueryWrapper<Order1> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("nor_id",nor_id);
        List<Order1> ordList = orderMapper.selectList(queryWrapper);
        if(ordList.size()==0){
            throw new OrderListNotFoundException("your order list not found");
            //没有查到，抛出异常
        }
        return ordList;
    }

    @Override
    public List<Order1> findOrderByAdmId(Integer adm_id) {
        QueryWrapper<Order1> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("adm_id",adm_id);
        List<Order1> ordList=orderMapper.selectList(queryWrapper);
        if(ordList.size()==0){
            throw new OrderListNotFoundException("order list not found");
            //没有查到，抛出异常
        }
        return ordList;
    }

    @Override
    public void DeliverPro(Integer ord_id, String log_Id,String Bus_name) {
        //log_id是快递单号，商品发货
        QueryWrapper<Order1> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ord_id",ord_id);
        Order1 order1 =orderMapper.selectOne(queryWrapper);
        if(order1 ==null){
            throw new OrderNotFoundException("not found order1");
            //抛出异常没找到订单
        }
        order1.setStatus2(3);
        order1.setLogId(log_Id);
        Date date=new Date();
        //数据库Order的问题，到时候重新写一下
        order1.setModifiedTime(date);
        order1.setModifiedUser(Bus_name);
        orderMapper.update(order1,queryWrapper);
        if(orderMapper.update(order1,queryWrapper)!=1){
            //返回值是0的时候是没有查到对应的数据
            //throw出去
            throw new OrderUpdateException("order1 update err-deliver");
        }
        return;
    }

    @Override
    public void RecieverPro(Integer ord_id) {
        QueryWrapper<Order1> queryWrapper=new QueryWrapper();
        queryWrapper.eq("ord_id",ord_id);
        Order1 order1 =orderMapper.selectOne(queryWrapper);
        if(order1 ==null){
            throw new OrderNotFoundException("not found order1 ");
            //抛出未找到异常

        }
        order1.setStatus2(4);
        Date date=new Date();

        //和上面的一样，是数据库的原因
        order1.setModifiedTime(date);
        orderMapper.update(order1,queryWrapper);
        if(orderMapper.update(order1,queryWrapper)!=1){
            //抛出修改异常
            throw new OrderUpdateException("order1 update err-receiver");
        }
        return;
    }
}
